/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.client.network;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
 *
 * @author Czarek
 */
public class NetworkStatsJMSProxy implements INetworkStats {

    private INetworkStats stats;
    private JmsTemplate jmsTemplate;
    private javax.jms.Queue queue;

    @Autowired
    public NetworkStatsJMSProxy(INetworkStats stats, JmsTemplate jmsTemplate, Queue queue) {
        this.stats = stats;
        this.jmsTemplate = jmsTemplate;
        this.queue = queue;
    }

    @Override
    public long countStatsFor(final int lastMilis) {
        return stats.countStatsFor(lastMilis);
    }
    
    private static class ReadMessageCreator implements MessageCreator{
        private final int bytes;

        public ReadMessageCreator(int read) {
            this.bytes = read;
        }
                
        @Override
        public Message createMessage(Session session) throws JMSException {
                final Message msg= session.createMessage();
                msg.setIntProperty("read", bytes);
                return msg;                
            }
    };

    @Override
    public void read(final int bytes) {
        jmsTemplate.send(new ReadMessageCreator(bytes));
    }
}
